<!-- 下载中心 -->
<template>
  <app-container>
    <app-block is-search>
      <zm-form inline :model="params" ref="searchForm" size="small">
        <zm-form-item label="导出时间" prop="startTime">
          <zm-date-picker
            :target="params"
            start-key="startTime"
            end-key="endTime"
          />
        </zm-form-item>
        <zm-form-item label="报表名称" prop="fileName">
          <el-input
            clearable
            v-model.trim="params.fileName"
            placeholder="请输入报表名称/券销售的号/券批次号"
            class="w290"
          ></el-input>
        </zm-form-item>
        <zm-form-item>
          <zm-button @click="onSearch" t="search"></zm-button>
          <zm-button @click="reset" t="reset"></zm-button>
        </zm-form-item>
      </zm-form>
      <zm-desc title="导出说明" type="warning" class="mb20">
        <template>
          <div>
            <p>
              由于导出文件生效后会暂存在阿里云oss上，文件只保留72小时，请尽快下载保存
            </p>
          </div>
        </template>
      </zm-desc>
    </app-block>
    <app-block>
      <zm-table-data
        :data="tableParams.data"
        :columns="columns"
        :fetch="loadList"
        :table-params="tableParams"
      ></zm-table-data>
    </app-block>
  </app-container>
</template>
<script>
import tableMixin from '@/mixins/zmTableMixin'
import { tableColumns } from './const'
import ZmDesc from '@/components/ZmDesc'
import exportTaskApis from '@/api/src/exportTask'
export default {
  components: { ZmDesc },
  mixins: [tableMixin],
  data () {
    return {
      params: {
        startTime: '',
        endTime: '',
        fileName: ''
      }
    }
  },
  computed: {
    columns () {
      return tableColumns(this)
    }
  },
  methods: {
    async loadList () {
      const _res = await exportTaskApis.getPageList(
        this.assignQuery({ ...this.params })
      )
      this.initialTableData(_res.list, _res.count)
    },
    reset () {
      this.$set(this.params, 'startTime', '')
      this.$set(this.params, 'endTime', '')
      this.onReset()
    },
    async onOperateClick (type, row) {
      let temp
      let name
      let link
      switch (type) {
        case 'down':
          temp = row.fileUrl.split('/')
          name =
            (temp.length && temp[temp.length - 1]) ||
            new Date().toLocaleString()
          link = document.createElement('a')
          link.style.display = 'none'
          link.href = row.fileUrl
          link.setAttribute('download', name)
          document.body.appendChild(link)
          link.click()
          this.$message.success('下载成功!')
          break
        case 'del':
          this.$confirm(`确定要删除${row.fileName}吗?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(async () => {
              await exportTaskApis.delete({ id: row.id })
              this.loadList()
              this.$message({
                message: '删除成功',
                type: 'success'
              })
            })
            .catch(() => {})
          break
      }
    }
  }
}
</script>
